package com.zhb.cloud.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
 * @ClassName: WebConfig
 * @Description:spring mvc的配置文件
 * @author: zhb1316783262@163.com
 * @date: 2019年4月15日下午3:59:41
 *
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
	/**
	 * JSON消息处理器
	 */
	public @Bean MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
		MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
		// 设置解析JSON工具类
		ObjectMapper objectMapper = new ObjectMapper();
		// 设置解析日期的工具类
		//objectMapper.setDateFormat();
		// 忽略未知属性 防止解析报错
		objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		jackson2HttpMessageConverter.setObjectMapper(objectMapper);
		List<MediaType> list = new ArrayList<>();
		list.add(MediaType.APPLICATION_JSON_UTF8);
		list.add(MediaType.APPLICATION_FORM_URLENCODED);
		list.add(MediaType.TEXT_PLAIN);
		jackson2HttpMessageConverter.setSupportedMediaTypes(list);
		return jackson2HttpMessageConverter;
	}
	/**
	 * 消息转换器
	 */
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		converters.add(mappingJackson2HttpMessageConverter());
		converters.add(mappingJackson2HttpMessageConverter());
		WebMvcConfigurer.super.configureMessageConverters(converters);
	}
}
